package com.fractal;

import java.util.concurrent.locks.*;

import android.graphics.Rect;
import android.util.Log;

public class Stack
{
    private java.util.Stack<Rect> collection;

    private Lock lock = new ReentrantLock();
    private Condition emptyQueue = lock.newCondition();

    public Stack()
    {
    	collection = new java.util.Stack<Rect>();
    }

    public void push( Rect t )
    {
    	lock.lock();
    	try
    	{
    		collection.push(t);
    		emptyQueue.signalAll();
    	}
    	finally
    	{
    		lock.unlock();
    	}
    }
    
    public Rect pop() throws InterruptedException
    {
        lock.lock();
        try
        {
            while ( collection.empty())
            {
            	Log.d("profile","Empty stack " + System.currentTimeMillis());
                emptyQueue.await();
            }
            Rect res = collection.pop();

            return res;
        }
        finally
        {
            lock.unlock();
        }            
    }
    
    public void clean()
    {
    	lock.lock();
    	try
    	{
    		collection.clear();
    	}
    	finally
    	{
    		lock.unlock();
    	}
    }
    
    public int size()
    {
    	int res = 0;
    	lock.lock();
    	try
    	{
    		res = collection.size();
    	}
    	finally
    	{
    		lock.unlock();
    	}
    	return res;
    }

}